package jmm;

import com.baomidou.mybatisplus.extension.api.R;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @ClassName jmm02
 * @date 2021/4/2 14:33
 * @Version 1.0
 * @Description //MESI机制和synchronized锁
 * @USER root
 */
public class jmm02 {
    private static int total = 0;
    private static Object object= new Object();
    private static ReentrantLock lock = new ReentrantLock();
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(1);

        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                try {
                    //让所有线程一块等待
                    countDownLatch.await();
                    for (int j = 0; j < 1000; j++) {
                        synchronized (object) {
                            total++;
                        }
                    }
                }catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();
        }
        Thread.sleep(1000);
        //开始运行
        countDownLatch.countDown();
        Thread.sleep(2000);
        System.out.println(total);
    }
}
